package com.alibaba.basic.controller;

import com.alibaba.basic.dto.CodeDto;
import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.service.IVerifyCodeService;
import com.alibaba.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){
        try {
           String base64Str =  verifyCodeService.imageVerifyCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试");
        }

    }
    @PostMapping("/smsCode")
    public AjaxResult phoneVerifyCode(@RequestBody CodeDto codeDto){
        try {
            String base64Str =  verifyCodeService.phoneVerifyCode(codeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            // 处理业务异常
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试");
        }

    }
    /**
     * 微信绑定时发送的手机验证码
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderVerifyCode(@RequestBody CodeDto codeDto){
        try {
            verifyCodeService.binderVerifyCode(codeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            //后端打印错误日志
            //e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

}
